package MainFunctionsGUIS;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.*;
import java.util.Vector;
import MainFunctionsDAOS.*;
import Password.*;


public class BookGUI extends JPanel implements ActionListener
{
	private Box xBox1, xBox2, xBox3, yBox1, yBox2, yBox3, yBox4;
	private JButton searchB = null;
	private JComboBox jcb1 = null;
	private JTextField jtf1 = null;
	
	//customer attributes
	private JLabel custNameL, custSurnameL, custIdL,
					custStatusL, custCreditL, custPhoneL,
					custBookingsL;
	private JLabel custName1L, custSurname1L, custId1L,
					custStatus1L, custCredit1L, custPhone1L,
					custBookings1L;
					
	//customer dialog components
	private JDialog custDialog = null;
	private JButton dialogAddB, dialogCancelB;
	private JTable custTable = null;
	//private JDialog custBookingsDialog = null;
	private JButton showBookingsB = null, deleteB = null,
					cancel1B = null;
	private JTable custBookingsTable = null;
	private JDialog bookingsDialog = null;
	private String custId = null;
	private JScrollPane jsp = null;
	
	//Check Out Attributes
	private DefaultTableModel bookModel = null;
	private JTable bookTable = null;
	private JButton removeB, cancelB, confirmB;
	private JLabel totalLabelL, totalAmountL;
	
	private StockItemDAO dao = null;
	
	
	

	public BookGUI() 
	{
		this.setName("BookGUI");
		dao = new StockItemDAO();
		custBookingsTable = new JTable();
		//xBox1
		xBox1 = new Box(BoxLayout.X_AXIS);
		removeB = new JButton("REMOVE");
		removeB.addActionListener(this);
		xBox1.add(removeB);
		cancelB = new JButton("CLEAR");
		cancelB.addActionListener(this);
		xBox1.add(cancelB);
		confirmB = new JButton("CONFIRM");
		confirmB.addActionListener(this);
		xBox1.add(confirmB);
		
		//yBox1
		yBox1 = new Box(BoxLayout.Y_AXIS);
		JLabel checkOutL = new JLabel("BOOK", JLabel.CENTER);
		//checkOutL.setPreferredSize(new Dimension(100,20));
		checkOutL.setBackground(Color.BLACK);
		checkOutL.setForeground(Color.WHITE);
		checkOutL.setOpaque(true);
		checkOutL.setMaximumSize(new Dimension(Short.MAX_VALUE, 20));
		checkOutL.setPreferredSize(new Dimension(Short.MAX_VALUE, 20));
		JPanel checkOutP = new JPanel(new GridLayout(1,1));
		checkOutP.add(checkOutL);
		yBox1.add(checkOutP);
		
		//setup tableModel
		bookModel = new DefaultTableModel();
		bookTable = new JTable(bookModel);
		bookModel.addColumn("stockId");
		bookModel.addColumn("Title");
		bookModel.addColumn("Price");
		bookModel.addColumn("Date");
		
		JScrollPane checkOutJSP = new JScrollPane(bookTable);
		yBox1.add(checkOutJSP);
		JPanel totalAmountPanel = new JPanel(new GridLayout(1,2));
		totalAmountPanel.setAlignmentX(JPanel.LEFT_ALIGNMENT);
		totalLabelL = new JLabel("TOTAL: ");
		totalAmountPanel.add(totalLabelL);
		totalAmountL = new JLabel("");
		totalAmountPanel.add(totalAmountL);
		totalAmountPanel.setPreferredSize(new Dimension(Short.MAX_VALUE, 30));
		totalAmountPanel.setBorder(BorderFactory.createEtchedBorder());
		yBox1.add(totalAmountPanel);
		yBox1.add(xBox1);
		yBox1.setPreferredSize(new Dimension(400,250));
		yBox1.setBorder(BorderFactory.createEtchedBorder());
		
		//xBox2
		xBox2 = new Box(BoxLayout.X_AXIS);
		jtf1 = new JTextField(20);
		//jtf1.setPreferredSize(new Dimension(80, 30));
		xBox2.add(jtf1);
		String [] jcb1String = new String[]{"Card", "Name"};
		jcb1 = new JComboBox(jcb1String);
		xBox2.add(jcb1);
		searchB = new JButton("SEARCH!");
		searchB.addActionListener(this);
		xBox2.add(searchB);
		xBox2.setPreferredSize(new Dimension(Short.MAX_VALUE, 30));
		
		//Group Panel
		custNameL = new JLabel("Name: ");
		custSurnameL = new JLabel("Surname: ");
		custIdL = new JLabel("CustId: ");
		custPhoneL = new JLabel("Phone No.: ");
		custCreditL = new JLabel("Credit Amount: ");
		custStatusL = new JLabel("Status: ");
		custBookingsL = new JLabel("Bookings: ");
		
		custName1L = new JLabel(" ");
		custSurname1L = new JLabel(" ");
		custId1L = new JLabel(" ");
		custPhone1L = new JLabel(" ");
		custCredit1L = new JLabel(" ");
		custStatus1L = new JLabel(" ");
		custBookings1L = new JLabel(" ");
		
		JPanel groupPanel = new JPanel();
		GroupLayout layout = new GroupLayout(groupPanel);
		groupPanel.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
		hGroup.addGroup(layout.createParallelGroup().
			addComponent(custNameL).addComponent(custSurnameL).
			addComponent(custIdL).addComponent(custPhoneL).
			addComponent(custCreditL).addComponent(custStatusL).
			addComponent(custBookingsL));
		hGroup.addGroup(layout.createParallelGroup().
			addComponent(custName1L).addComponent(custSurname1L).
			addComponent(custId1L).addComponent(custPhone1L).
			addComponent(custCredit1L).addComponent(custStatus1L).
			addComponent(custBookings1L));
		layout.setHorizontalGroup(hGroup);
		
		GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
		vGroup.addGroup(layout.createParallelGroup().
			addComponent(custNameL).addComponent(custName1L));
		vGroup.addGroup(layout.createParallelGroup().
			addComponent(custSurnameL).addComponent(custSurname1L));
		vGroup.addGroup(layout.createParallelGroup().
			addComponent(custIdL).addComponent(custId1L));
		vGroup.addGroup(layout.createParallelGroup().
			addComponent(custPhoneL).addComponent(custPhone1L));
		vGroup.addGroup(layout.createParallelGroup().
			addComponent(custCreditL).addComponent(custCredit1L));
		vGroup.addGroup(layout.createParallelGroup().
			addComponent(custStatusL).addComponent(custStatus1L));
		vGroup.addGroup(layout.createParallelGroup().
			addComponent(custBookingsL).addComponent(custBookings1L));
		layout.setVerticalGroup(vGroup);
		
		//xBox3
		xBox3 = new Box(BoxLayout.X_AXIS);
		JLabel imageL = new JLabel(new ImageIcon("c:/Java_Stuff(VzapCD)/Images/IconImages/Bird.gif"));
		imageL.setBorder(BorderFactory.createEtchedBorder());
		xBox3.add(imageL);
		xBox3.add(groupPanel);
		xBox3.setPreferredSize(new Dimension(300,Short.MAX_VALUE));
		
		
		
		//yBox2
		yBox2 = new Box(BoxLayout.Y_AXIS);
		showBookingsB = new JButton(" Show Bookings ");
		showBookingsB.addActionListener(this);
		JLabel custL = new JLabel("CUSTOMER", JLabel.CENTER);
		custL.setPreferredSize(new Dimension(Short.MAX_VALUE,20));
		custL.setBackground(new Color(255,50,50));
		custL.setForeground(Color.WHITE);
		custL.setOpaque(true);
		JPanel custP = new JPanel(new GridLayout(1,1));
		custP.add(custL);
		yBox2.add(custP);
		yBox2.add(xBox2);
		yBox2.add(xBox3);
		yBox2.add(showBookingsB);
		yBox2.setPreferredSize(new Dimension(270,250));
		yBox2.setBorder(BorderFactory.createEtchedBorder());
		
		//this
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		this.add(yBox2);
		this.add(yBox1);
		this.setPreferredSize(new Dimension(Short.MAX_VALUE, 250));
		
		//setup buttons for customer dialog
		dialogAddB = new JButton("ADD");
		dialogAddB.addActionListener(this);
		dialogCancelB = new JButton("CANCEL");
		dialogCancelB.addActionListener(this);
	}//end constructor
	

	
	
	public void actionPerformed(ActionEvent ae)
	{
		Object source = ae.getSource();
		if(source == searchB && jcb1.getSelectedItem() == "Card")
		{
			StockItemDAO customerDAO = new StockItemDAO();
			custTable = new JTable();
			custTable = customerDAO.getCustomerByCard(jtf1.getText());
			if(custTable.getRowCount() > 1)
			{
				custDialog = new JDialog();
				custDialog.setLayout(new BorderLayout());
				JPanel dialogButtonPanel = new JPanel(new GridLayout(1,2));
				dialogButtonPanel.add(dialogAddB);
				dialogButtonPanel.add(dialogCancelB);
				JScrollPane custJSP = new JScrollPane(custTable);
				custJSP.setHorizontalScrollBarPolicy(
					ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
				custJSP.setVerticalScrollBarPolicy(
					ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
				custDialog.add(custJSP, BorderLayout.CENTER);
				custDialog.add(dialogButtonPanel, BorderLayout.SOUTH);
				custDialog.setSize(500,300);
				custDialog.setLocationRelativeTo(null);
				custDialog.setVisible(true);
			}
			else
			{
				populateCustLabels(0);
			}
			
		}
		if(source == dialogAddB)
		{
			populateCustLabels(custTable.getSelectedRow());
			custDialog.dispose();
		}
		if(source == dialogCancelB)
		{
			custDialog.dispose();
		}
		if(source == removeB)
		{
			try
			{
				bookModel.removeRow(bookTable.getSelectedRow());
				updateBookTotal();
			}
			catch(IndexOutOfBoundsException ioe)
			{
				if(bookTable.getRowCount() < 1)
				{
					JOptionPane.showMessageDialog(yBox1,
					 	" You must first add items \nbefore you " +
					 	"can remove them.");
				}
				else
				{
					JOptionPane.showMessageDialog(yBox1, 
						" You must first select\n an item to remove.");
				}
				
			}
			
		}
		if(source == cancelB)
		{
			int rowCount = bookTable.getRowCount();
			for(int i = 0; i < rowCount; i++)
			{
				bookModel.removeRow(0);
			}
			updateBookTotal();
		}
		if(source == confirmB)
		{
			int bookNum = 0;
			int bookRows = 0;
			int bookTot = 0;
			if(custBookings1L.getText().compareTo(" ") > 0)
			{
				bookNum = Integer.parseInt(custBookings1L.getText());
				bookRows = bookTable.getRowCount();
				bookTot = bookNum + bookRows;
			}
			
			if(custName1L.getText().equals(" "))
			{
				JOptionPane.showMessageDialog(yBox2, "NO CUSTOMER SELECTED!");
			}
			else if(bookTable.getRowCount() == 0)
			{
				JOptionPane.showMessageDialog(yBox1, "NO ITEMS TO BOOK!");
			}
			else if(bookTot > 3)
			{
				if(bookNum == 3)
					JOptionPane.showMessageDialog(this, "Oops! This customer already has 3 " +
						"bookings. \nYou will have to delete some if you wish to continue.");
				else if(bookNum == 2)
					JOptionPane.showMessageDialog(this, "Oops! This customer already has 2 " +
						"bookings. \nYou can only make one more.");
				else if(bookNum == 1)
					JOptionPane.showMessageDialog(this, "Oops! This customer already has 1 " +
						"bookings. \nYou can only make 2 more.");
				else
					JOptionPane.showMessageDialog(this, "Oops! Maximum amount of bookings " +
						"exceeded.");		
			}
			else
			{
				double saleValue = Double.parseDouble(totalAmountL.getText());
				String custId = custId1L.getText();
				Vector stockIdVect = new Vector();
				Vector dateVect = new Vector();
				for(int i = 0; i < bookTable.getRowCount(); i++)
				{
					stockIdVect.addElement(bookTable.getValueAt(i,0));
					dateVect.addElement(bookTable.getValueAt(i,3));
				}
				
				dao.makeBooking(saleValue, custId, "emp001",
						 stockIdVect, dateVect);
				removeRows();
			}
		}//end of confirmB
		
		//SHOW BOOKINGS
		//-------------
		if(source == showBookingsB)
		{
			deleteB = new JButton(" DELETE ");
			deleteB.addActionListener(this);
			cancel1B = new JButton(" CANCEL ");
			cancel1B.addActionListener(this);
			JPanel deleteP = new JPanel();
			deleteP.add(deleteB);
			deleteP.add(cancel1B);
			custId = custId1L.getText();
			bookingsDialog = new JDialog();
			custBookingsTable = dao.getCustBookings(custId);
			jsp = new JScrollPane(custBookingsTable);
			bookingsDialog.add(jsp, BorderLayout.CENTER);
			bookingsDialog.add(deleteP, BorderLayout.SOUTH);
			bookingsDialog.setSize(320,200);
			bookingsDialog.setLocationRelativeTo(null);
			bookingsDialog.setModal(true);
			bookingsDialog.setVisible(true);
		}
		if(source == deleteB)
		{
			int row = custBookingsTable.getSelectedRow();
			String custId = (String)custBookingsTable.getValueAt(row, 0);
			String stockId = (String)custBookingsTable.getValueAt(row, 1);
			String dateBooked = (String)custBookingsTable.getValueAt(row, 3);
			dao.deleteBooking(custId, stockId, dateBooked);
			JOptionPane.showMessageDialog(bookingsDialog, "Booking deleted.");
			bookingsDialog.dispose();
			showBookingsB.doClick();
		}
		if(source == cancel1B)
		{
			bookingsDialog.dispose();
		}
		
				
	}//end of actionPerformed-------------
	
	public void populateCustLabels(int rowIndex)
	{
		try
		{
			custId1L.setText((String)custTable.getValueAt(rowIndex,0));
			custName1L.setText((String)custTable.getValueAt(rowIndex,1));
			custSurname1L.setText((String)custTable.getValueAt(rowIndex,2));
			custStatus1L.setText((String)custTable.getValueAt(rowIndex,3));
			custCredit1L.setText((String)custTable.getValueAt(rowIndex,4));
			custPhone1L.setText((String)custTable.getValueAt(rowIndex,5));
			custBookings1L.setText("" + dao.getBookings(
				(String)custTable.getValueAt(rowIndex,0)));
			this.validate();
		}
		catch(IndexOutOfBoundsException ioe)
		{
			JOptionPane custOptionPane = new JOptionPane();
			custOptionPane.setRootFrame(null);
			custOptionPane.showMessageDialog(yBox2, "No matching customers were found\n" +
							" where " + jcb1.getSelectedItem() + " starts with \"" +
							jtf1.getText() + "\"");
		}
		
	}
	
	//add selection from mainGui to BOOK section
	public void updateBookTable(Object [] row)
	{
		int i = 0;
		boolean duplicateFlag = false;
		boolean bookLimitFlag = false;
		while(i < bookTable.getRowCount())
		{
			if(row[0].equals(bookTable.getValueAt(i,0)))
			{
				JOptionPane.showMessageDialog(yBox1, "You may not add the same stock " +
					"\nitem more than once.", "DUPLICATE ENTRY", JOptionPane.WARNING_MESSAGE);
				duplicateFlag = true;
			}
			
			i++;
		}
		if(bookTable.getRowCount() > 2)
		{
			JOptionPane.showMessageDialog(yBox1, "You may not book more than " +
				"\n3 items.", "DUPLICATE ENTRY", JOptionPane.WARNING_MESSAGE);
			bookLimitFlag = true;
		}	
		if(duplicateFlag == false && bookLimitFlag == false)
		{
			bookModel.addRow(row);
		}
	}
	public void updateBookTotal()
	{
		double total = 0;
		for(int i = 0; i < bookTable.getRowCount(); i++)
		{
			total = total + Double.parseDouble(bookTable.getValueAt(i, 2).toString());
		}
		totalAmountL.setText(Double.toString(total));
	}
	
	public void removeRows()
	{
		int rowCount = bookTable.getRowCount();
			for(int i = 0; i < rowCount; i++)
			{
				bookModel.removeRow(0);
			}
			updateBookTotal();
	}
}
